var t = require;
var e = module;
var o = exports;
var a =
    (this && this.__spreadArrays) ||
    function () {
        for (var t = 0, e = 0, o = arguments.length; e < o; e++) t += arguments[e].length;
        var a = Array(t),
            i = 0;
        for (e = 0; e < o; e++) for (var n = arguments[e], r = 0, s = n.length; r < s; r++, i++) a[i] = n[r];
        return a;
    };
Object.defineProperty(o, "__esModule", {value: !0});
null == window.wx && null != window.tt && (window.wx = window.tt);
var i = new ((function () {
    function t() {
        this.defPath = "/";
        this.musicPath = "background";
        this.lastMusicPath = "";
        this.audioBundle = "AudioBundle";
        this.volumeChangeCall = 0;
        this.canPlayMusic = !0;
        this.playing_music = !1;
        this.playing_music_name = void 0;
        this.maxVolume = 1;
        this.effectAudioId = {};
        this.file_path_map = new Map();
        this.audio_clip_map = {};
        this.audio_path_map = {};
        this.playIntervalLimitSec = 0.08;
        this._playIntervalTally = 1;
        this.effectIntervalMap = new Map();
        setInterval(this._addPlayIntervalTime.bind(this), 20);
    }
    t.prototype._addPlayIntervalTime = function () {
        this._playIntervalTally += 0.02;
    };
    t.prototype.setPlayIntervalLimitSec = function (t) {
        this.playIntervalLimitSec = t;
    };
    t.prototype.pushAudioIntervalLimit = function (t) {
        if (Array.isArray(t))
            for (var e = 0; e < t.length; e++) {
                var o = t[e];
                this.effectIntervalMap.set(o, 0);
            }
        else this.effectIntervalMap.set(t, 0);
    };
    t.prototype.popAudioIntervalLimit = function (t) {
        if (Array.isArray(t))
            for (var e = 0; e < t.length; e++) {
                var o = t[e];
                this.effectIntervalMap.delete(o);
            }
        else this.effectIntervalMap.delete(t);
    };
    t.prototype.setAudioBundle = function (t) {
        this.audioBundle = t;
    };
    t.prototype.preloadAudio = function (t) {
        var e = this;
        this.loadRes(this.audioBundle, this.defPath + t, cc.AudioClip, function (o) {
            if (o) {
                var a = o;
                e.audio_clip_map[t] = a;
            }
        });
    };
    t.prototype.releaseAudio = function (t) {
        if (this.audio_clip_map[t]) {
            var e = cc.loader,
                o = e.getDependsRecursively(this.audio_clip_map[t]);
            e.release(o);
            delete this.audio_clip_map[t];
        }
    };
    t.prototype.switchoverAudio = function () {
        this.switchoverAudioMusic();
        this.switchoverAudioEffect();
    };
    t.prototype.switchoverAudioMusic = function () {
        this.setPlayMusic(!this.isPlayMusic());
    };
    t.prototype.switchoverAudioEffect = function () {
        this.setPlayEffects(!this.isPlayEffects());
    };
    t.prototype.isPlayMusic = function () {
        var t = window;
        void 0 === t.canPlayMusic && (t.canPlayMusic = this.readBool("hly_bgm", !0));
        return t.canPlayMusic;
    };
    t.prototype.isPlayEffects = function () {
        var t = window;
        void 0 === t.canPlayEffects && (t.canPlayEffects = this.readBool("hly_me", !0));
        return t.canPlayEffects;
    };
    t.prototype.setPlayMusic = function (t) {
        if (this.isPlayMusic() != t) {
            this.write("hly_bgm", t ? 1 : 0);
            window.canPlayMusic = t;
            this.playMusic();
        }
    };
    t.prototype.setPlayEffects = function (t) {
        if (this.isPlayEffects() != t) {
            this.write("hly_me", t ? 1 : 0);
            window.canPlayEffects = t;
        }
    };
    t.prototype.readBool = function (t, e) {
        var o = cc.sys.localStorage.getItem(t);
        return o ? "false" != o && ("true" == o || 1 == o) : e;
    };
    t.prototype.replaceMusic = function (t) {
        this.playMusic(t);
    };
    t.prototype.write = function (t, e) {
        null != t && null != e && cc.sys.localStorage.setItem(t, "" + e);
    };
    t.prototype.playMusic = function (t, e) {
        void 0 === e && (e = 1);
        if (this.isPlayMusic()) {
            if (t == this.lastMusicPath && cc.audioEngine.isMusicPlaying()) return !0;
            0 == (t = t || this.lastMusicPath).length && (t = this.musicPath);
            this.lastMusicPath = t;
            this.playing_music = !0;
            this.playing_music_name = t;
            return this._playMusicAudio(t, !0, e);
        }
        t && 0 != t.length && (this.lastMusicPath = t);
        this.stopMusic();
        return !1;
    };
    t.prototype._playMusicAudio = function (t, e, o) {
        var a = this;
        void 0 === e && (e = !1);
        void 0 === o && (o = 1);
        var i = !1;
        if (this.audio_clip_map[t])
            try {
                this._setMusicVolume(o);
                this.doPlayMusic(this.audio_clip_map[t], e);
                i = !0;
            } catch (t) {
                cc.log(t);
            }
        else {
            var n = this;
            this.loadRes(this.audioBundle, this.defPath + t, cc.AudioClip, function (o) {
                try {
                    if (o) {
                        n.audio_clip_map[t] = o;
                        n.audio_clip_map[t + ".mp3"] = o;
                        n.isPlayMusic() && a.doPlayMusic(o, e);
                    }
                } catch (t) {
                    cc.log(t);
                }
            });
        }
        return i;
    };
    t.prototype.doPlayMusic = function (t, e) {
        if (t && this.isPlayMusic()) {
            t.once ||
                (t.once = function (e, o, i) {
                    var n = function () {
                        for (var r = [], s = 0; s < arguments.length; s++) r[s] = arguments[s];
                        t.off(e, n, i);
                        o.call.apply(o, a([i], r));
                    };
                    t.on(e, n, i);
                });
            cc.audioEngine.playMusic(t, e);
        }
    };
    t.prototype.setMusicMaxVolme = function (t) {
        this.maxVolume = t;
    };
    t.prototype.setMusicVolum = function (t, e, o) {
        void 0 === e && (e = !1);
        void 0 === o && (o = 100);
        this.setMusicVolume(t, e, o);
    };
    t.prototype.setMusicVolume = function (t, e, o) {
        void 0 === e && (e = !1);
        void 0 === o && (o = 100);
        (t = Math.min(t, this.maxVolume)) > 0 && !cc.audioEngine.isMusicPlaying() && this.playMusic();
        var a = this;
        if (a.volumeChangeCall) {
            clearInterval(a.volumeChangeCall);
            a.volumeChangeCall = 0;
        }
        if (e)
            a.volumeChangeCall = setInterval(function () {
                var e = a.getMusicVolume();
                if (Math.abs(e - t) < 0.1) {
                    a._setMusicVolume(t);
                    if (a.volumeChangeCall) {
                        clearInterval(a.volumeChangeCall);
                        a.volumeChangeCall = 0;
                    }
                } else {
                    cc.audioEngine.isMusicPlaying();
                    a._setMusicVolume(e + (e > t ? -0.1 : 0.1));
                }
            }, o);
        else {
            cc.audioEngine.isMusicPlaying();
            a._setMusicVolume(t);
        }
    };
    t.prototype._setMusicVolume = function (t) {
        cc.audioEngine.setMusicVolume(t);
    };
    t.prototype.getMusicVolume = function () {
        return cc.audioEngine.getMusicVolume();
    };
    t.prototype.stopMusic = function (t, e) {
        var o = this;
        void 0 === t && (t = !1);
        void 0 === e && (e = 100);
        this.playing_music = !1;
        if (t)
            var a = setInterval(function () {
                if (o.getMusicVolume() > 0.1) o._setMusicVolume(o.getMusicVolume() - 0.1);
                else {
                    cc.audioEngine.stopMusic();
                    clearInterval(a);
                }
            }, e);
        else cc.audioEngine.stopMusic();
    };
    t.prototype.pauseMusic = function () {
        cc.audioEngine.pauseMusic();
        this.playing_music = !1;
    };
    t.prototype.resumeMusic = function () {
        cc.audioEngine.resumeMusic();
        this.playing_music = !0;
    };
    t.prototype.play = function (t, e, o) {
        void 0 === e && (e = !1);
        void 0 === o && (o = 1);
        return this.playEffect(t, e, o);
    };
    t.prototype.playEffect = function (t, e, o) {
        void 0 === e && (e = !1);
        void 0 === o && (o = 1);
        return this.isPlayEffects() ? this._playAudio(t, e, o) : 0;
    };
    t.prototype.stopEffect = function (t) {
        var e = this.effectAudioId[t];
        if (null != e) {
            delete this.effectAudioId[t];
            cc.audioEngine.stop(e);
        }
    };
    t.prototype.pauseEffect = function (t) {
        var e = this.effectAudioId[t];
        null != e && cc.audioEngine.pauseEffect(e);
    };
    t.prototype.resumeEffect = function (t) {
        var e = this.effectAudioId[t];
        null != e && cc.audioEngine.resumeEffect(e);
    };
    t.prototype._playAudio = function (t, e, o) {
        void 0 === e && (e = !1);
        void 0 === o && (o = 1);
        var a = this.effectIntervalMap.get(t);
        if (void 0 !== a) {
            if (!(a <= this._playIntervalTally)) return 0;
            this.effectIntervalMap.set(t, this._playIntervalTally + this.playIntervalLimitSec);
        }
        if (this.audio_clip_map[t]) {
            this.effectAudioId[t] = cc.audioEngine.play(this.audio_clip_map[t], e, o);
            return this.effectAudioId[t] || 0;
        }
        var i = this;
        this.loadRes(this.audioBundle, this.defPath + t, cc.AudioClip, function (a) {
            if (a) {
                i.audio_clip_map[t] = a;
                i.isPlayEffects() && (i.effectAudioId[t] = cc.audioEngine.play(a, e, o));
            }
        });
        return 0;
    };
    t.prototype.pauseAllEffects = function () {
        cc.audioEngine.pauseAll();
    };
    t.prototype.resumeAllEffects = function () {
        cc.audioEngine.resumeAll();
    };
    t.prototype.stopAllEffects = function () {
        cc.audioEngine.stopAllEffects();
    };
    t.prototype.invokeAction = function (t) {
        for (var e = [], o = 1; o < arguments.length; o++) e[o - 1] = arguments[o];
        t && t.apply(void 0, e);
    };
    t.prototype.loadRes = function (t, e, o, i) {
        var n = this,
            r = cc;
        if (r.assetManager) {
            "/" != e[0] && (e = "/" + e);
            var s = r.assetManager.getBundle(t),
                c = s ? s.get(e, o) : null;
            c
                ? this.invokeAction(i, c)
                : r.assetManager.loadBundle(t, function (a, r) {
                      r &&
                          r.load(e, o, function (e, o) {
                              if (o) n.invokeAction(i, o);
                              else {
                                  cc.warn(t, e);
                                  n.invokeAction(i, null);
                              }
                          });
                  });
        } else
            cc.loader.loadRes(e, cc.AudioClip, function (t, e) {
                var o = Array.isArray(e) ? e[0] : e;
                o &&
                    (o.once ||
                        (o.once = function (t, e, i) {
                            var n = function () {
                                for (var r = [], s = 0; s < arguments.length; s++) r[s] = arguments[s];
                                o.off(t, n, i);
                                e.call.apply(e, a([i], r));
                            };
                            o.on(t, n, i);
                        }));
                n.invokeAction(i, o);
            });
    };
    return t;
})())();
window.AudioManager = i;
o.default = i;
